package com.xl333.calc.jFrame;

import com.xl333.calc.Calc;
import com.xl333.calc.OperatorEnum;
import com.xl333.util.PropertiesUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;

/**
 * @auth liux
 * @date 2021/6/18
 */
public class MainFrame {

    private Calc calc = new Calc();

    private static MainFrame instance = new MainFrame();

    private MainFrame(){

    }

    public static MainFrame getInstance(){
        return instance;
    }

    /**
     * 初始化窗口
     */
    public void initFrame() throws UnsupportedEncodingException {
        JFrame frame = new JFrame();
        frame.setTitle(PropertiesUtil.getChineseValue("application.name"));
//        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);  //设置布局为空布局，避免button设置大小不生效

        //文本域
        JPanel panel = new JPanel();
        panel.setSize(390, 500);
        JTextField textField = createTextField();
        panel.add(textField);

        panel.add(createGridPanel(textField));

        frame.add(panel);

        //设置界面可见
        frame.setBounds(50, 50, 405, 500);
        frame.setVisible(true);
    }

    /**
     * 创建一个面板
     * @return
     */
    private JPanel createGridPanel(JTextField textField){
        JPanel panel = new JPanel();
        panel.setSize(390, 390);
        panel.setLayout(new GridLayout(5, 4, 5, 5));

        //第一行按钮
        panel.add(createOperatorButton(OperatorEnum.PERCENT, textField));
        panel.add(createOperatorButton(OperatorEnum.CE, textField));
        panel.add(createOperatorButton(OperatorEnum.C, textField));
        panel.add(createOperatorButton(OperatorEnum.DELETE, textField));

        //第二行按钮
        panel.add(createNumberButton(7, textField));
        panel.add(createNumberButton(8, textField));
        panel.add(createNumberButton(9, textField));
        panel.add(createOperatorButton(OperatorEnum.DIVIDE, textField));

        //第三行按钮
        panel.add(createNumberButton(4, textField));
        panel.add(createNumberButton(5, textField));
        panel.add(createNumberButton(6, textField));
        panel.add(createOperatorButton(OperatorEnum.MULTIPLY, textField));

        //第四行按钮
        panel.add(createNumberButton(1, textField));
        panel.add(createNumberButton(2, textField));
        panel.add(createNumberButton(3, textField));
        panel.add(createOperatorButton(OperatorEnum.SUBTRACT, textField));

        //第五行按钮
        panel.add(createNumberButton(0, textField));
        panel.add(createOperatorButton(OperatorEnum.POINT, textField));
        panel.add(createOperatorButton(OperatorEnum.EQUAL, textField));
        panel.add(createOperatorButton(OperatorEnum.ADD, textField));

        return panel;
    }

    /**
     * 创建一个文本域
     * @return
     */
    private JTextArea createTextArea(){
        JTextArea textArea = new JTextArea();
        textArea.setRows(2);
        textArea.setColumns(25);
        textArea.setLineWrap(true); //自动换行
        return textArea;
    }

    /**
     * 创建一个文本域
     * @return
     */
    private JTextField createTextField(){
        JTextField textField = new JTextField();
        textField.setColumns(19);
        textField.setHorizontalAlignment(JTextField.RIGHT);
        return textField;
    }

    /**
     * 创建一个数字按钮
     * @param i
     * @return
     */
    private JButton createNumberButton(int i, JTextField textField){
        return createButton(i + "", new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                calc.inputNumber(i);
                textField.setText(calc.toString());
            }
        });
    }

    private JButton createOperatorButton(OperatorEnum operatorEnum, JTextField textField){
        return createButton(operatorEnum.getOperator(), new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                calc.inputOperator(operatorEnum);
                textField.setText(calc.toString());
            }
        });
    }

    /**
     * 创建一个按钮
     * @param text
     * @param actionListener
     * @return
     */
    private JButton createButton(String text, ActionListener actionListener){
        JButton button = new JButton();
        button.setText(text);
        button.addActionListener(actionListener);
        button.setSize(60, 30);
        return button;
    }

}
